<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;

class CollectionApiRequest extends Model
{
    //接口类型
    const API_TYPE_RELIEF = 4;//减免
    const API_TYPE_WITHHOLDING = 1;//代扣
    const API_TYPE_ACCOUNT = 2;//销账
    const API_TYPE_REPAYMENT = 3;//全部结清

    //记录状态
    const STATUS_RESPONSE_OK = 1;//响应成功
    const STATUS_RECEIPT_OK = 2;//回执成功

    //响应code
    const RESPONSE_OK = 1000;//成功
    const RESPONSE_PARAM_NOT_FAND = 1010;//缺少参数
    const RESPONSE_OTHER_ERROR = 1040;//其他错误
    const RESPONSE_SIGN_ERROR = 1111;//签名错误

    //代扣类型
    const WITHHOLDING_ZERO=0;//提前结清
    const WITHHOLDING_ONE=1;//还1期
    const WITHHOLDING_TWO=2;//还多期

    // 销账类型
    const WRITEOFF_ZERO = 0; //提前结清
    const WRITEOFF_ONE=1;//还1期
    const WRITEOFF_TWO=2;//还多期



    protected $guarded = ['id'];



    /**
     * Description:创建数据
     * Author:lijiafei
     * @param $responseData array
     */
    public static function createApiRequestData($responseData){
        $createData = array();
        $createData['request_time'] = Carbon::now()->toDateTimeString();
        $createData['request_data'] = json_encode(request()->post(),JSON_UNESCAPED_UNICODE);
        $createData['response_data'] = json_encode($responseData,JSON_UNESCAPED_UNICODE);
        $createData['status'] = self::STATUS_RESPONSE_OK;
        $data = self::getCreateApiData();
        $createData['api_type'] = $data['api_type'];
        $createData['apply_number'] = $data['apply_number'];
        $responseCode = self::getResponseCode($responseData);
        $createData['response_code'] = $responseCode;
        self::create($createData);

    }

    /**
     * Description:获取请求类型和流水号
     * Author:lijiafei
     * @return array
     */
    protected static function getCreateApiData(){
        $action = request()->route()->action;
        $apiType = 0;
        switch ($action['as']){
            case 'collection.repayment':
                $apiType = self::API_TYPE_REPAYMENT;
                break;
            case 'collection.repayrelief.add':
                $apiType = self::API_TYPE_RELIEF;
                break;
            default:
                break;
        }
        $param = request()->all();
        $param = json_decode($param['content'],true);
        $applyNumber = isset($param['applyNumber']) ? $param['applyNumber'] : '';
        $returnData = [
            'api_type' => $apiType,
            'apply_number' => $applyNumber
        ];
        return $returnData;
    }

    protected static function getResponseCode(array $responseData){
        if(isset($responseData['result'])){
            return $responseData['result'];
        }
        return 0;
    }

    /**
     * Description:获取减免金额
     * Author:zlk
     * @return int
     */
    public static function getDerate($loan_id,$period){
         $derate_money=RepayReliefRecordModel::where('loan_id',$loan_id)
             ->where('period',$period)
             ->where('status',1)
             ->sum('relief_amount');
         return bcdiv($derate_money,100,2);
    }
}
